package com.yxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yxh.base.PageEntity;
import com.yxh.dao.ProjectMapper;
import com.yxh.pojo.Menu;
import com.yxh.pojo.Project;
import com.yxh.service.ProjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * Created by asus on 2018/6/1.
 */

@Service
@Transactional
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public int save(Project record) {
        try {
            return projectMapper.insert(record);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int update(Project record) {

        try {
            return projectMapper.updateByPrimaryKeySelective(record);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int delete(String id) {
        try {
            return projectMapper.deleteByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public void batchDelete(List<Project> records) {

    }

    @Override
    public Project get(String id) {
        return null;
    }

    @Override
    public Project get(Project record) {
        return null;
    }

    @Override
    public List<Project> getList(Project record) {
        return null;
    }

    @Override
    public List<Project> getAllList(Project record) {
        return null;
    }

    @Override
    public List<Project> getListPage(PageEntity<Project> record) {
        return null;
    }

    @Override
    public PageInfo getInformation(Project record, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
      /*  try {
            if (!StringUtils.isEmpty(menu.getMenuName())) {
                menu.setMenuName(new String(menu.getMenuName().getBytes("iso8859-1"), "utf-8"));
            }

            reflectTest(record);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        List<Map<String,Object>> menuList =projectMapper.selectByProject(record);
        PageInfo pageInfo = new PageInfo(menuList);
        pageInfo.setList(menuList);
        return pageInfo;


    }


    public Project reflectTest(Project model)  {
        try{
            Field[] field = model.getClass().getDeclaredFields();
            // 遍历所有属性
            for (int j = 0; j < field.length; j++) {
                // 获取属性的名字
                String name = field[j].getName();
                // 将属性的首字符大写，方便构造get，set方法
                name = name.substring(0, 1).toUpperCase() + name.substring(1);
                // 获取属性的类型
                String type = field[j].getGenericType().toString();
                // 如果type是类类型，则前面包含"class "，后面跟类名
                System.out.println("属性为：" + name);
                if (type.equals("class java.lang.String")) {
                    Method m = model.getClass().getMethod("get" + name);
                    // 调用getter方法获取属性值
                    String value = (String) m.invoke(model);
                    System.out.println("数据类型为：String");
                    if (value != null) {
                        System.out.println("属性值为：" + value);
                    } else {
                        System.out.println("属性值为：空");
                    }
                }

            }
        }catch(Exception e){
           e.printStackTrace();
        }

        return model;
    }


    @Override
    public PageInfo getInformationApp(String value, Integer limit, Integer offset) {
        PageHelper.startPage(limit, offset);

        List<Map<String,Object>> menuList =projectMapper.selectByProjectApp(value);
        PageInfo pageInfo = new PageInfo(menuList);
        pageInfo.setList(menuList);
        return pageInfo;
    }
}
